package com.gem.growingtree.activity;

import java.math.BigDecimal;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.gem.growingtree.bodyline.BodyData;
import com.gem.growingtree.bodyline.BodyDataService;

public class BodyLineActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_body_line);
		final EditText editText = (EditText) findViewById(R.id.high);
		final EditText editText1 = (EditText) findViewById(R.id.weight);
		final EditText editText2 = (EditText) findViewById(R.id.time);
		Button button = (Button) findViewById(R.id.submit);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				double h = Double.parseDouble(editText.getText().toString());
				double w = Double.parseDouble(editText1.getText().toString());
				// 点的纵坐标
				// float bmi = (float) (h / w);
				float bmi1 = (float) (w / (h * h));
				BigDecimal bigDecimal = new BigDecimal(bmi1);
				float bmi = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP)
						.floatValue();
				String time = editText2.getText().toString();

				// 确定点的横坐标
				int a = Integer.parseInt(time.substring(0, 4));
				int b = Integer.parseInt(time.substring(5, time.length()));
				int xx = 0;
				int key = a % 2014;
				switch (key) {
				case 0:
					xx = b;
					break;
				default:
					xx = b + 12;
					break;
				}
				// 存入数据
				BodyData bodyData = new BodyData(xx, bmi);
				BodyDataService bodyDataService = new BodyDataService(
						BodyLineActivity.this);
				try {
					// 添加类
					bodyDataService.addData(bodyData);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				Intent intent = new Intent(BodyLineActivity.this,
						BodyActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.body_line, menu);
		return true;
	}

}
